﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace printimage
{
    class Program
    {
        static private string VERSION = "1.9";
        static int Main(string[] args)
        {
            if (args.Length == 1)
            {
                if (args[0].ToLower() == "-p")
                {
                    PrintPrinters();
                }
                else if (args[0].ToLower() == "-r")
                {
                    PrintResultDoc();
                }
                else
                {
                    PrintUsage();
                }
                Console.WriteLine("按任意键退出");
                Console.ReadKey();
                return 0;
            }
            else if (args.Length == 2)
            {
                PrintParam param = getCmd(args);
                if (param == null)
                {
                    PrintUsage();
                    Console.WriteLine("按任意键退出");
                    Console.ReadKey();
                    return 0;
                }
                return HandlePrint(param);
            }
            else
            {
                PrintUsage();
                Console.WriteLine("按任意键退出");
                Console.ReadKey();
            }
            return 0;
        }

        static int HandlePrint(PrintParam param)
        {
            PrintHelper helper = new PrintHelper();
            PrintHelper.ResultCode ret = helper.Print(param.src , param.printerNo);
            Console.WriteLine(getErroDesc(ret));
            return (int)ret;
        }

        static string getErroDesc(PrintHelper.ResultCode rc)
        {
            string ret = "未知错误";
            switch (rc)
            {
                case PrintHelper.ResultCode.Unknown:
                    ret = "未知错误";
                    break;
                case PrintHelper.ResultCode.InvalidFile:
                    ret = "无效文件";
                    break;
                case PrintHelper.ResultCode.InvalidParam:
                    ret = "无效参数";
                    break;
                case PrintHelper.ResultCode.PrinterInvalid:
                    ret = "打印机不可用";
                    break;
                case PrintHelper.ResultCode.Succeeded:
                    ret = "打印成功";
                    break;
                case PrintHelper.ResultCode.UserCanceled:
                    ret = "取消打印";
                    break;
            }
            return ret;
        }

        static void PrintResultDoc()
        {
            string desc = "============= 图片打印程序 , 版本:" + VERSION + " ===================\n";
            desc += "返回值说明:\n";
            desc += ((int)PrintHelper.ResultCode.Unknown).ToString() + " : " + getErroDesc(PrintHelper.ResultCode.Unknown) + "\n";
            desc += ((int)PrintHelper.ResultCode.Succeeded).ToString() + " : " + getErroDesc(PrintHelper.ResultCode.Succeeded) + "\n";
            desc += ((int)PrintHelper.ResultCode.InvalidParam).ToString() + " : " + getErroDesc(PrintHelper.ResultCode.InvalidParam) + "\n";
            desc += ((int)PrintHelper.ResultCode.InvalidFile).ToString() + " : " + getErroDesc(PrintHelper.ResultCode.InvalidFile) + "\n";
            desc += ((int)PrintHelper.ResultCode.PrinterInvalid).ToString() + " : " + getErroDesc(PrintHelper.ResultCode.PrinterInvalid) + "\n";
            desc += ((int)PrintHelper.ResultCode.UserCanceled).ToString() + " : " + getErroDesc(PrintHelper.ResultCode.UserCanceled) + "\n";
            Console.Write(desc);
        }

        static void PrintUsage()
        {
            string usage = "============= 图片打印程序 , 版本:" + VERSION + " ===================\n";
            usage += "使用方法:\n";
            usage += "显示参数 -p 显示本机打印机，本参数单独使用\n";
            usage += "打印参数 1: -s源文件路径 , 2: -i打印机序列号，序列号可通过-p参数查看\n";
            usage += @"示例: printimage.exe -sC:\abc\a.jpg -i2" + "\n";
            usage += "返回代码 : -r\n";
            Console.Write(usage);
        }

        static void PrintPrinters()
        {
            PrintHelper helper = new PrintHelper();
            var list = helper.GetAllPrinters();
            var count = list.Count;
            if (count == 0)
            {
                Console.WriteLine("未找到任何打印机。");
                return;
            }
            string info = "本机打印列表:\n";
            for (var i = 0; i < count; i++)
            {
                info += (i + 1).ToString() + " : " + list[i] + "\n";
            }
            Console.Write(info);
        }

        static PrintParam getCmd(string[] args)
        {
            int count = args.Length;
            PrintParam ret = new PrintParam();
            for (int i = 0; i < count; i++)
            {
                string arg = args[i];
                if (arg.Length < 2)
                    continue;
                arg = arg.ToLower();
                string pre = arg.Substring(0, 2);
                if (pre == "-s")
                {
                    //源文件
                    ret.src = arg.Substring(2);
                }
                if (pre == "-i")
                {
                    //源文件
                    int no;
                    if (int.TryParse(arg.Substring(2) , out no))
                    {
                        ret.printerNo = no;
                    }
                }
            }
            if (string.IsNullOrEmpty(ret.src) || ret.printerNo == 0)
            {
                return null;
            }
            return ret;
        }
    }

    class PrintParam
    {
        public string src;
        public int printerNo;
    }
}
